import qdWebSocket from '../qdWebSocket'
import config from '../../config/apiConfig'
const mixins = {
  // 定义data数据
  data() {
    return {
      refreshDataWS: null
    }
  },
  methods: {
    // 注册ws
    registerWS() {
      const vm = this
      const wsUrl = config.api.refreshDataWebSocket
      const userId = session.getUserInfo().user_id
      vm.refreshDataWS = qdWebSocket.initQDWS(`${wsUrl}/${userId}`)
      qdWebSocket.registerListner(vm.refreshDataWS, {
        open: vm.websocketonopen,
        error: vm.websocketonerror,
        message: vm.websocketonmessage,
        close: vm.websocketclose
      })
      return vm.refreshDataWS
    },
    websocketonopen() {
      console.info('连接成功')
    },
    // 连接建立失败重连
    websocketonerror() {
      console.info('连接失败')
    },
    // 数据接收后执行的操作
    websocketonmessage(e) {
      try {
        const info = JSON.parse(e)
        console.info(`消息推送===${info}`)
        eventBus.$emit('wsRefreshAssemblyData', info) // 更改数据
      } catch (event) {
        console.info(`消息推送========${event}`)
      }
    },
    // 发送消息
    websocketsend(Data) {
      // 数据发送
      console.info(`数据发送========${Data}`)
      this.refreshDataWS.send(JSON.stringify(Data))
    },
    // 关闭连接
    websocketclose(e) {
      // 关闭
      console.log('断开连接', e)
    }
  }
}
export default mixins
